﻿using System.Text;

namespace WemoConnect1
{
    public class SetDbVersionCommand : WemoCommand
    {
        private int _version;
        protected string Command;
        protected string Argument;

        public SetDbVersionCommand(int version)
        {
            _version = version;
            Command = "SetRulesDBVersion";
            Argument = "RulesDBVersion";
        }

        protected override void Execute()
        {
            var responsebuf = new byte[51200];

            var request = GetCommand(WemoSocket.RemoteEndPoint.ToString());
            SendRequest(request);
            ReceiveRequestState();
            ReceiveResponse();
        }

        string GetCommand(string authority)
        {
            var body = GetCommandBody();

            var contentBuilder = new StringBuilder();
            contentBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            contentBuilder.Append("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
            contentBuilder.Append("<s:Body>");
            contentBuilder.Append(body);
            contentBuilder.Append("</s:Body>");
            contentBuilder.Append("</s:Envelope>");
            var content = contentBuilder.ToString();

            var header = GetCommandHeader(authority, content.Length);

            return header + content;
        }

        private string GetCommandHeader(string authority, int contentLength)
        {
            var headerBuilder = new StringBuilder();
            headerBuilder.AppendLine("POST /upnp/control/rules1 HTTP/1.1");
            headerBuilder.AppendLine("SOAPACTION: \"urn:Belkin:service:rules:1#" + Command + "\"");
            headerBuilder.AppendLine("Host: " + authority);
            headerBuilder.AppendLine("Content-type: text/xml; charset=\"utf-8\"");
            headerBuilder.AppendLine("Content-Length: " + contentLength);
            headerBuilder.AppendLine(); // End of HTTP header
            var header = headerBuilder.ToString();
            return header;
        }

        private string GetCommandBody()
        {
            var bodyBuilder = new StringBuilder();
            bodyBuilder.Append("<m:" + Command + " xmlns:m=\"urn:Belkin:service:rules:1\">");
            if (!string.IsNullOrEmpty(Argument))
            {
                bodyBuilder.Append("<" + Argument + ">");
                bodyBuilder.Append(_version);
                bodyBuilder.Append("</" + Argument + ">");
            }
            bodyBuilder.Append("</m:" + Command + ">");
            var body = bodyBuilder.ToString();
            return body;
        }
    }

    public class GetDbVersionCommand : SetDbVersionCommand
    {
        public GetDbVersionCommand()
            : base(0)
        {
            Command = "GetRulesDBVersion";
            Argument = null;
        }
    }
}
